package com.alangyun.remind.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alangyun.remind.error.MindException;
import com.alangyun.remind.service.ILeaderService;
import com.alangyun.remind.service.IUploadService;
import com.alangyun.remind.vo.DataResponse;
import com.alangyun.remind.vo.ErrorCode;
import com.alangyun.remind.vo.FilterParam;
import com.alangyun.remind.vo.Response;
import com.alangyun.remind.vo.leader.LeaderView;

/**
 * 干部管理模块
 *
 * @class com.alangyun.remind.controller.LeaderController
 * @company 北京诚朗信息技术有限公司
 * @author hoobort klxukun@126.com
 * @datetime 2022年11月10日 下午1:33:51
 *
 */
@Controller
public class LeaderController extends AbstractController {
	@Resource
	private IUploadService svc;
	@Resource
	private ILeaderService svcData;

	/**
	 * 干部队伍预警功能页面
	 *
	 * @author hoobort klxukun@126.com
	 * @company 北京诚朗信息技术有限公司
	 * @datetime 2022年11月10日 下午1:23:52
	 * @return
	 */
	@RequestMapping(value=Paths.LeaderUrl.LEADER_LEAD11, method= {RequestMethod.GET, RequestMethod.POST})
	public String leadPage11(Model model){
		String ret = "/mind/leader-analysis-1-1";
		try {
			validateTimeout();
			
			model.addAttribute(CategoryIdentities.VARIABLE, CategoryIdentities.LEADER_TEAM);
//			model.addAttribute("navigate",svcData.categories());
		}catch(MindException me) {
			ret = pageError(model, me.getMessage());
			e(me.getMessage(), me);
		}catch(Exception ex) {
			ret = pageError(model, "打开离退干部预警管理页面出错了，可能原因 " + ex.getMessage());
			e("打开离退干部预警管理页面出错", ex);
		}
		
		return ret;
	}
	
	@RequestMapping(value=Paths.LeaderUrl.LEADER_LEAD12, method= {RequestMethod.GET, RequestMethod.POST})
	public String leadPage12(Model model) {
		String ret = "/mind/leader-analysis-1-2";
		try {
			validateTimeout();
			
			model.addAttribute(CategoryIdentities.VARIABLE, CategoryIdentities.LEADER_TEAM);
//			model.addAttribute("navigate", svcData.categories());
		} catch (MindException me) {
			ret = pageError(model, me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret = pageError(model, "打开干部预警管理页面出错了，可能原因 " + ex.getMessage());
			e("打开干部预警管理页面出错", ex);
		}

		return ret;
	}
	
	@PostMapping(value = Paths.LeaderUrl.LEADER_UPLOAD)
	@ResponseBody
	public Response leaderUpload(@RequestParam("file") MultipartFile file) {
		Response ret = new Response();
		try {
			validateTimeout();
			
			svc.parseLeaderFile(file);
			ret.setCode(ErrorCode.SUCCESS);
		}catch(MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc("上传管理干部数据预警数据处理失败");
			e("上传管理干部数据预警数据处理失败", ex);
		}
		return ret;
	}
	

	@PostMapping(value = Paths.LeaderUrl.LEADER_LEAD11_SUM)
	@ResponseBody
	public DataResponse<LeaderView> queryLeader(@RequestParam(value="team", required= false)Integer[] teams,
			@RequestParam(value="prof", required= false)Integer[] profs,
			@RequestParam(value="job", required= false)Integer[] jobs) {
		DataResponse<LeaderView> ret = new DataResponse<LeaderView>();
		try {
			validateTimeout();
			
			ret.setData(svcData.listLeaderRatio(teams, profs, jobs)).setCode(ErrorCode.SUCCESS);
		}catch(MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		}catch(Exception ex) {
			ret.setCode(ErrorCode.INNER_ERROR).setDesc("列出干部管理预警数据失败");
			e("列出干部管理预警数据失败",ex);
		}
		
		return ret;
	} 

	@PostMapping(value = Paths.LeaderUrl.LEADER_LEAD12_SUM)
	@ResponseBody
	public DataResponse<LeaderView> queryTired(@RequestParam(value="team", required= false)Integer[] teams,
			@RequestParam(value="prof", required= false)Integer[] profs,
			@RequestParam(value="job", required= false)Integer[] jobs) {
		DataResponse<LeaderView> ret = new DataResponse<LeaderView>();
		try {
			validateTimeout();
			
			ret.setData(svcData.listTiredRatio(teams, profs, jobs)).setCode(ErrorCode.SUCCESS);
		}catch(MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		}catch(Exception ex) {
			ret.setCode(ErrorCode.INNER_ERROR).setDesc("列出离退干部预警数据失败");
			e("列出离退干部预警数据失败",ex);
		}
		
		return ret;
	} 


	@PostMapping(value = Paths.LeaderUrl.LEADER_NAV_TREE)
	@ResponseBody
	public DataResponse<FilterParam> categories() {
		DataResponse<FilterParam> ret = new DataResponse<FilterParam>();
		try {
			validateTimeout();
			
			ret.setData(svcData.categories());
			ret.setCode(ErrorCode.SUCCESS);
		} catch (MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc("提取过滤参数失败");
			e("提取过滤参数失败", ex);
		}

		return ret;
	}
}
